package com.prs.crm.action.official;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.domain.official.MobileSMS;
import com.prs.crm.service.official.MobileSMSService;

public class MobileSMSAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private MobileSMS mobileSMS;
	private MobileSMSService mobileSMSService;

	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public MobileSMS getMobileSMS() {
		return mobileSMS;
	}
	public void setMobileSMS(MobileSMS mobileSMS) {
		this.mobileSMS = mobileSMS;
	}
	public MobileSMSService getMobileSMSService() {
		return mobileSMSService;
	}
	public void setMobileSMSService(MobileSMSService mobileSMSService) {
		this.mobileSMSService = mobileSMSService;
	}
	@Action("editMobileSMS")
	public String editMobileSMS(){
		if (ids!=null) {
			this.mobileSMS=this.getMobileSMSService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action("viewMobileSMS")
	public String viewMobileSMS(){
		return SUCCESS;
	}
	@Action("editForwardMobileSMS")
	public String editForwardMobileSMS(){
		mobileSMS.setContent(mobileSMS.getContent());
		return SUCCESS;
	}
	@Action("listUnsentMobileSMS")
	public String listUnsentMobileSMS(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(MobileSMS.class);
		criteria.add(Restrictions.eq("state", 1));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMobileSMSService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("listsentMobileSMS")
	public String listsentMobileSMS(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(MobileSMS.class);
		criteria.add(Restrictions.eq("state", 2));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMobileSMSService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action(value = "saveMobileSMS", results = {
			@Result(name = "success", type = "redirect", location = "editMobileSMS"),
			@Result(name = "error", type = "chain", location = "editMobileSMS") })
	public String saveMobileSMS(){
		Date da=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		if (mobileSMS.getId()==null) {
			mobileSMS.setState(1);
			mobileSMS.setTime(sf.format(da));
			this.getMobileSMSService().save(mobileSMS);
		}else{
			mobileSMS.setTime(sf.format(da));
			this.getMobileSMSService().submit(mobileSMS);
		}
		return SUCCESS;
	}
	@Action(value = "saveMobileSMSs", results = {
			@Result(name = "success", type = "redirect", location = "listUnsentMobileSMS"),
			@Result(name = "error", type = "chain", location = "editForwardMobileSMS") })
	public String saveMobileSMSs(){
		Date da=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		mobileSMS.setState(1);
		mobileSMS.setTime(sf.format(da));
		this.getMobileSMSService().save(mobileSMS);
		return SUCCESS;
	}
	@Action(value = "removeMobileSMS", results = {
			@Result(name = "success", type = "redirect", location = "listUnsentMobileSMS"),
			@Result(name = "error", type = "chain", location = "listUnsentMobileSMS") })
	public String removeMobileSMS(){
		if (ids!=null) {
			this.mobileSMS=this.getMobileSMSService().get(ids[0]);
		}
		this.getMobileSMSService().batchRemove(ids);
		return SUCCESS;
	}
}
